﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.LocDieuKien
{
    public partial class frmLocDieuKien : Form
    {
        #region khởi tạo
        bool checkClose = false;
        DateTime date;
        public static string truoc;
        public static string sau;
        string batdau, ketthuc;
        public static bool kiemtra = false;

        public frmLocDieuKien()
        {
            InitializeComponent();
            date = Entitystatic.date;
            dtpkDenNgay.Text = dtpkNgay.Text = dtpkTuNgay.Text = date.ToString("MM/dd/yyyy");
            cbbNamTheoNam.Text = cbbNamTheoQuy.Text = cbbNamTheoThang.Text = date.Year.ToString();
            cbbQuyTheoQuy.Text = "1";
            cbbThangTheoThang.Text = date.Month.ToString();
        }
        #endregion

        #region lọc điều kiện
        void LocDieuKien()
        {
            try
            {
                if (!Validate())
                {
                    return;
                }
                checkClose = true;
                kiemtra = true;
                if (rdbTimKiemTheoNgay.Checked == true)
                {
                    truoc = sau = dtpkNgay.Text;
                    this.Close();
                }
                if (rdbTimKiemTheoThang.Checked == true)
                {
                    batdau = Common.Utilities.MyDateConversion("01/" + cbbThangTheoThang.Text + "/" + cbbNamTheoThang.Text);
                    if (cbbThangTheoThang.Text == "12")
                        ketthuc = Common.Utilities.MyDateConversion("01/01/" + (Convert.ToInt32(cbbNamTheoThang.Text) + 1).ToString());
                    else
                        ketthuc = Common.Utilities.MyDateConversion("01/" + (Convert.ToInt32(cbbThangTheoThang.Text) + 1).ToString() + "/" + cbbNamTheoThang.Text);
                    truoc = batdau;
                    sau = ketthuc;
                    this.Close();
                }
                if (rdbTimKiemTheoQuy.Checked == true)
                {
                    if (cbbQuyTheoQuy.Text == "1")
                    {
                        batdau = "01/01/" + cbbNamTheoQuy.Text;
                        ketthuc = "04/01/" + cbbNamTheoQuy.Text;
                    }
                    else if (cbbQuyTheoQuy.Text == "2")
                    {
                        batdau = "04/01/" + cbbNamTheoQuy.Text;
                        ketthuc = "07/01/" + cbbNamTheoQuy.Text;
                    }
                    else if (cbbQuyTheoQuy.Text == "3")
                    {
                        batdau = "07/01/" + cbbNamTheoQuy.Text;
                        ketthuc = "10/01/" + cbbNamTheoQuy.Text;
                    }
                    else
                    {
                        batdau = "10/01/" + cbbNamTheoQuy.Text;
                        ketthuc = "01/01/" + (int.Parse(cbbNamTheoQuy.Text) + 1).ToString();
                    }
                    truoc = batdau;
                    sau = ketthuc;
                    this.Close();
                }
                if (rdbTimKiemTheoNam.Checked == true)
                {
                    truoc = "01/01/" + cbbNamTheoNam.Text;
                    sau = "01/01/" + (int.Parse(cbbNamTheoNam.Text) + 1).ToString();
                    this.Close();
                }
                if (rdbTimKiemTheoThoiGian.Checked == true)
                {
                    truoc = dtpkTuNgay.Text;
                    sau = dtpkDenNgay.Text;
                    this.Close();
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {

            this.Close();
        }
        #endregion

        #region check visible
        void CheckVisible()
        {
            lbNgay.Visible = dtpkNgay.Visible =
            dtpkDenNgay.Visible = dtpkTuNgay.Visible =
            lbThangTheoThang.Visible = lbNamTheoNam.Visible =
            lbNamTheoThang.Visible = lbQuyTheoQuy.Visible =
            lbTuNgay.Visible = lbDenNgay.Visible =
            cbbNamTheoNam.Visible = cbbNamTheoQuy.Visible =
            cbbNamTheoThang.Visible = cbbQuyTheoQuy.Visible =
            cbbThangTheoThang.Visible = lbNamTheoQuy.Visible = false;

        }
        #endregion

        #region Event
        private void frmLocDieuKien_Load(object sender, EventArgs e)
        {
            this.Closing += new CancelEventHandler(frmLocDieuKien_Closing);
            btnLocDieuKien.Click += new EventHandler(btnLocDieuKien_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void btnLocDieuKien_Click(object sender, EventArgs e)
        {
            LocDieuKien();
        }

        void frmLocDieuKien_Closing(object sender, CancelEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }

        void KeyDown_Chung(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyData == Keys.Enter)
                {
                    LocDieuKien();
                }
                if (e.KeyData == Keys.Escape)
                {
                    Thoat();
                }
            }
            catch
            {
            }
        }

        void rdbTimKiem_Checked(object sender, EventArgs e)
        {
            try
            {
                RadioButton temp = (RadioButton)sender;
                switch (temp.Name)
                {

                    case Common.Constants.rdbTimKiemTheoNgay:
                        {
                            CheckVisible();
                            lbNgay.Visible = dtpkNgay.Visible = true;

                            break;
                        }
                    case Common.Constants.rdbTimKiemTheoThang:
                        {
                            CheckVisible();

                            lbThangTheoThang.Visible = lbNamTheoThang.Visible =
                            cbbThangTheoThang.Visible = cbbNamTheoThang.Visible = true;
                            break;
                        }
                    case Common.Constants.rdbTimKiemTheoNam:
                        {
                            CheckVisible();

                            lbNamTheoNam.Visible = cbbNamTheoNam.Visible = true;

                            break;
                        }
                    case Common.Constants.rdbTimKiemTheoQuy:
                        {
                            CheckVisible();

                            lbQuyTheoQuy.Visible = lbNamTheoQuy.Visible =
                            cbbQuyTheoQuy.Visible = cbbNamTheoQuy.Visible = true;

                            break;
                        }
                    case Common.Constants.rdbTimKiemTheoThoiGian:
                        {
                            CheckVisible();

                            lbTuNgay.Visible = lbDenNgay.Visible =
                            dtpkDenNgay.Visible = dtpkTuNgay.Visible = true;

                            break;
                        }
                    default:
                        break;
                }
            }
            catch
            {
            }
        }
        #endregion
    }
}
#endregion